package com.ncmmall.mall.controller.business.order;

import com.ncmmall.domain.business.goods.Goods;
import com.ncmmall.domain.business.order.aftersale.OrderAfterSale;
import com.ncmmall.domain.business.order.aftersale.QOrderAfterSale;
import com.ncmmall.domain.security.user.QWebUser;
import com.ncmmall.domain.security.user.WebUser;
import com.ncmmall.mall.params.PageParams;
import com.ncmmall.querychannel.QueryPage;
import com.ncmmall.querychannel.QueryPageRequest;
import com.ncmmall.querychannel.service.QueryChannelService;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import java.util.List;
import java.util.Map;

/**
 * @author linxs
 * @Description: 售后订单管理
 * @Date: 2019-07-10 17:44
 */
@Controller
@RequestMapping(value = "/mall/orderaftersale")
public class OrderAfterSaleController {

    @Autowired
    private QueryChannelService queryChannelService;

    /**
     * 订单列表
     */
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    private String list(Model model, PageParams pageParams, OrderAfterSale order) {
        QueryPageRequest pageRequest = new QueryPageRequest().setPageNo(pageParams.getPageNo());
        Map<String, Object> params = Maps.newHashMap();
        String hql = builderSQL("select u from OrderAfterSale u where 1=1 ", params, order);
        QueryPage<OrderAfterSale> page = queryChannelService.findPage(hql, pageRequest.setParams(params), OrderAfterSale.class);
        List<WebUser> merchants = queryChannelService.findAll(QWebUser.webUser.role.name.eq("MERCHANT"), WebUser.class);

        model.addAttribute("page", page).addAttribute("params", order).addAttribute("merchants", merchants);

        return "mall/order/aftersale/list";
    }


    /**
     * 查看详情
     */
    @RequestMapping(value = "/{id}/detail", method = RequestMethod.GET)
    private String detail(Model model, @PathVariable("id") long id) {

        OrderAfterSale afterSale = queryChannelService.findOne(QOrderAfterSale.orderAfterSale.id.eq(id), OrderAfterSale.class);
        List<Goods> giftGoodses = Lists.newArrayList();

        //赠品活动订单
        if (afterSale.getOrder().getMarketingType() == 1) {
            giftGoodses = queryChannelService.findAll("select g.goods from GiftGoods g where g.gift.id = :id ", ImmutableMap.of("id", afterSale.getOrder().getGiftActivity().getId()), Goods.class);
        }

        model.addAttribute("giftgoodses", giftGoodses);
        model.addAttribute("afterSale", afterSale);
        return "mall/order/aftersale/detail";
    }


    /**
     * 条件筛选
     */
    private String builderSQL(String sql, Map<String, Object> params, OrderAfterSale order) {

        if (order.getMerchant() != null && order.getMerchant().getId() > 0) {
            sql += " and u.merchant.id = :merchantId ";
            params.put("merchantId", order.getMerchant().getId());
        }

        if (order.getMember() != null && order.getMember().getId() > 0) {
            sql += " and u.member.id = :memberId ";
            params.put("memberId", order.getMember().getId());
        }

        if (order.getStatus() > 0) {
            sql += " and u.status = :status ";
            params.put("status", order.getStatus());
        }

        sql += " order by u.createTime desc ";

        return sql;
    }

}
